package com.sdgakj.web.h5;

import com.google.common.collect.Maps;
import com.sdgakj.dto.base.ResCode;
import com.sdgakj.dto.base.ResResult;
import com.sdgakj.entity.News;
import com.sdgakj.entity.User;
import com.sdgakj.service.ServiceException;
import com.sdgakj.service.feed.FeedService;
import com.sdgakj.service.news.NewsService;
import com.sdgakj.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springside.modules.web.MediaTypes;
import org.springside.modules.web.Servlets;

import javax.servlet.ServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by win10 on 2017/5/24.
 */
@Controller
@RequestMapping("/h5/news")
public class H5NewsController {
    private static final String PAGE_SIZE = "10";
    private static Map<String,String> sortTypes = Maps.newLinkedHashMap();

    static {
        sortTypes.put("auto", "自动");
    }
    @Autowired
    private NewsService newsService;

    @Autowired
    private UserService userService;

    @Autowired
    private FeedService feedService;
    /**
     * 获取全部news
     * */
    @RequestMapping(value = "/getNewsAll",method = RequestMethod.GET)
    public String getNewsAll(@RequestParam(value = "page", defaultValue = "1") int pageNumber,
                             @RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
                             @RequestParam(value = "sortType", defaultValue = "auto") String sortType,
                             Model model,ServletRequest request){
        Map<String,Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        Page<News> newss =newsService.getNews(searchParams,pageNumber,pageSize,sortType);
        model.addAttribute("newslist",newss);
        return  "/h5/find/news";
    }

    /**
     * 新闻轮播图临时跳转页面
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/getNewStatic",method = RequestMethod.GET)
    public String getNewStatic(@RequestParam(value = "userId",defaultValue = "1")long userId,Model model,ServletRequest request){
        Long newId = 14L;
        News news=newsService.getNewsById(newId);
        User user=userService.getUserById(userId);
        model.addAttribute("newDetail",news);
        model.addAttribute("user",user);
        return  "/h5/find/find_NewsDetail";
    }

    @RequestMapping(value = "getNewsById")
    public String getNewsById(Long newId,@RequestParam(value = "userId",defaultValue = "1")long userId,Model model){
        News news=newsService.getNewsById(newId);
        User user=userService.getUserById(userId);
        model.addAttribute("newDetail",news);
        model.addAttribute("user",user);
        return "h5/find/find_NewsDetail";
    }
    @RequestMapping(value = "addNewsComment",method = RequestMethod.GET, produces = MediaTypes.JSON_UTF_8)
    public ResResult createComment(@RequestParam(value = "content", required = true) String content,
                                   @RequestParam(value = "id", required = false) Long id) {
        ResResult result = new ResResult("评论已发布");
        try {
            result.setData(newsService.createComment(1L,1L,id,content));
        } catch (ServiceException e) {
            result.setResCode(e.getResCode());
        } catch (Exception e) {
            result.setResCode(ResCode.ERROR);
        }
        return result;
    }
}
